iris = csvread('iris.txt');

D = iris(:, 1:4);
L = iris(:,5);

c = 1;
g = 1/4;

cvp = cvpartition(size(D,1), 'kfold', 10);

errs_1v1 = zeros(cvp.NumTestSets,1);
errs_1vR = zeros(cvp.NumTestSets,1);

for p = 1:cvp.NumTestSets
    train = D(cvp.training(p), :);
    trainLabels = L(cvp.training(p));
    
    test = D(cvp.test(p), :);
    testLabels = L(cvp.test(p));
    
    params = ['-s 0 -t 2 -g ', g, ' -c ', c, ' -q'];
    
    model_1v1 = svmtrain(trainLabels, train, params);   % one vs. one SVM
    model_1vR = ovrtrain(trainLabels, train, params);   % one vs. rest SVM
    
    [predict_1v1] = svmpredict(testLabels, test, model_1v1);
    [predict_1vR ac decv] = ovrpredict(testLabels, test, model_1vR);
    
    for x = 1:size(test,1)
        if (predict_1v1(x) ~= testLabels(x))
            errs_1v1(p,1) = errs_1v1 + 1;
        end
        if (predict_1vR(x) ~= testLabels(x))
            errs_1vR(p,1) = errs_1vR + 1;
        end
    end 
    errs_1v1(p,1) = errs_1v1(p,1) / size(test,1);
    errs_1vR(p,1) = errs_1vR(p,1) / size(test,1);
end

crossValError_1v1 = [mean(errs_1v1), var(errs_1v1)];
crossValError_1vR = [mean(errs_1vR), var(errs_1vR)];